﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.IService.Design;
using Zhp.Model.Common;
using Zhp.Model.Design;

namespace Zhp.Api.Controllers.Design
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Design)]
    [SeedMenu(MenuGroup = ApiGroupEnum.Design, Name = "表单管理", Order = 2, RouterName = "designform", WebFilePath = "design/designForm/index", Icon = "form-setting")]
    public class DesignFormController : ControllerBase
    {
        private IDesignFormService _designFormService;
        public DesignFormController(IDesignFormService designFormService)
        {
            _designFormService = designFormService;
        }

        [HttpPost]
        [SeedList("表单列表", "表单管理", ApiGroupEnum.Design)]
        public async Task<PageRet<DesignFormModel>> Query(DesignFormQuery p)
        {
            var list = await _designFormService.Query(p);
            return new PageRet<DesignFormModel>
            {
                DataList = list,
                TotalCount = p.TotalCount,
            };
        }

        [HttpPost]
        [SeedButton("新增", "表单列表", "表单管理", ApiGroupEnum.Design)]
        public async Task<bool> Create(DesignFormEdit p)
        {
            return await _designFormService.Create(p);
        }

        [HttpPut]
        [SeedButton("修改", "表单列表", "表单管理", ApiGroupEnum.Design)]
        public async Task<bool> Update(DesignFormEdit p)
        {
            return await _designFormService.Update(p);
        }

        [HttpDelete("{id}")]
        [SeedButton("删除", "表单列表", "表单管理", ApiGroupEnum.Design)]
        public async Task<bool> Delete(long id)
        {
            return await _designFormService.Delete(id);
        }

        [HttpGet("{id}")]
        [SeedButton("详情", "表单列表", "表单管理", ApiGroupEnum.Design)]
        public async Task<DesignFormEdit> Details(long id)
        {
            return await _designFormService.Details(id);
        }

        [HttpGet]
        public async Task<List<DesignFormSelect>> Select()
        {
            return await _designFormService.Select();
        }

        /// <summary>
        /// 根据表单id查询所使用的表对应的字段
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        [HttpGet("{formId}")]
        public async Task<List<DesignFormFieldSelect>> QueryFieldSelect(long formId)
        {
            return await _designFormService.QueryFieldSelect(formId);
        }

        /// <summary>
        /// 根据表单id查询表单所有字段配置
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        [HttpGet("{formId}")]
        public async Task<List<DesignFormField>> QueryFormFieldConfig(long formId)
        {
            return await _designFormService.QueryFormFieldConfig(formId);
        }
    }
}
